﻿$(document).ready(function() {
    // function for validate registration form
    // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
    
    var tips = $( ".validateTips" );

    function updateTips( t ) {
        tips
            .text( t )
            .addClass( "ui-state-highlight" );
        setTimeout(function() {
            tips.removeClass( "ui-state-highlight", 1500 );
        }, 500 );
    }

    function checkLength( o, n, min, max ) {
        if ( o.val().length > max || o.val().length < min ) {
            o.addClass( "ui-state-error" );
            updateTips( n + " 的长度必须在 " +
                min + " 和 " + max + "之间。" );
            return false;
        } else {
            return true;
        }
    }

    function checkRegexp( o, regexp, n ) {
        if ( !( regexp.test( o.val() ) ) ) {
            o.addClass( "ui-state-error" );
            updateTips( n );
            return false;
        } else {
            return true;
        }
    }
    
    // registration dialog
    $( "#register" ).dialog({
        autoOpen: false,
        height: 550,
        width: 350,
        modal: true,
        buttons: {
            "注册": function() {
                var bValid = true,
					email = $( "#reg_email" ),
					password = $( "#reg_password" ),
					name = $( "#reg_name" ),
					location = $( "#reg_location" ),
                    dialog = $( this ),
					allFields = $( [] ).add( email ).add( password ).add( name ).add( location );
                
				allFields.removeClass( "ui-state-error" );

                bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "电子邮件好像不太对哦" );
                bValid = bValid && checkLength( password, "密码", 1, 40 );
                bValid = bValid && checkLength( name, "姓名昵称", 1, 50 );				
                bValid = bValid && checkLength( location, "所在地", 1, 30 );				

                if ( bValid ) {
					$.getJSON('/user/register/',{username:email.val(),password:password.val(),location:location.val(),name:name.val()}, function(ret){
						if (ret.msg == 'OK') {
							// register sucessfully
							$(this).dialog( "close" );
						}
						else {
							// unsucessful
							updateTips( ret.msg );
						}
					});
                }
            },
            "取消": function() {
				dialog.dialog( "close" );
			}
        }
    });

    // login dialog
    $( "#login" ).dialog({
        autoOpen: false,
        height: 400,
        width: 350,
        modal: true,
        buttons: {
            "登录": function() {
                var username = $( "#login_username" ),
					password = $( "#login_password" ),
                    dialog = $( this ),
					allFields = $( [] ).add( username ).add( password );
                
				allFields.removeClass( "ui-state-error" );

				$.post('/user/login/',
					{
					'username':username.val(),
					'password':password.val(),
					'csrfmiddlewaretoken':$( '#login input[name="csrfmiddlewaretoken"]' ).val()
					}, 
					function(ret){
						if (ret.msg == "OK") {
							// register sucessfully
							$(this).dialog( "close" );
							location.reload();							
						}
						else {
							// unsucessful
							updateTips( ret.msg );
						}
					},
					"json"
					);
            },
            "取消": function() {
				dialog.dialog( "close" );
			}
        }
    });

    // new film dialog
    $( "#new_film" ).dialog({
        autoOpen: false,
        height: 600,
        width: 400,
        modal: true,
        buttons: {
            "申领": function() {
                var bValid = true,
                    title = $( "#newfilm_title" ),
					description = $( "#newfilm_des" ),
                    number = $( "#newfilm_num" ),
                    right = $( "#newfilm_right" ),
                    dialog = $( this ),
					allFields = $( [] ).add( title ).add( description ).add( number ).add( right );
                    
                allFields.removeClass( "ui-state-error" );

                bValid = bValid && checkLength( title, "胶卷名", 1, 30 );
                
                if ( bValid ) {
                    $.post('/new_film/',{
                        'title':title.val(),
                        'description':description.val(),
                        'number':number.val(),
                        'right':right.val(),
                        'csrfmiddlewaretoken':$( '#new_film input[name="csrfmiddlewaretoken"]' ).val()
                        }, 
                        function(ret){
                            if (ret.msg == 'OK') {
                                // register sucessfully
                                dialog.dialog( "close" );
                            }
                            else {
                                // unsucessful
                                updateTips( ret.msg );
                            }
                        },
                        "json"
                    );
                }
            },
            "取消": function() {
				dialog.dialog( "close" );
			}
        }
    });
	
    // buttons
	$( ".button_common" ).button();
    $( ".user_handle button" ).button();
    $( "#button_register" ).click(function() {
        $( "#register" ).dialog( "open" );
    })
    $( "#button_login" ).click(function() {
        $( "#login" ).dialog( "open" );
    })	
	$( "#button_new_film" ).click(function() {
        $( "#new_film" ).dialog( "open" );
    })	
    $( "#button_logout" ).click(function() {
        $.get("/user/logout/");
		location.reload();
    })		
});
